package com.wyh.springbootweb.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.springbootweb.Util.RestHelper;
import com.wyh.springbootweb.dao.EmployeeMapper;
import com.wyh.springbootweb.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.*;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("addEmployee")
    public String addEmployee() {
        return "employee/add";
    }

    @PostMapping("addEmployee")
    public String addEmployeeHandler(Model model,
                                     @RequestParam(name = "name", required = true, defaultValue = "") String name,
                                     @RequestParam(name = "role", required = true, defaultValue = "") String role,
                                     @RequestParam(name = "sex", required = true, defaultValue = "1") String sex,
                                     @RequestParam(name = "birthday", required = true, defaultValue = "") String birthday,
                                     @RequestParam(name = "age", required = true, defaultValue = "1") Integer age
    ) {

        Date date = new Date();
        Employee employee = new Employee();
        employee.setName(name);
        employee.setRole(role);
        employee.setSex(sex);
        employee.setBirthday(birthday);
        employee.setAge(age);
        employee.setCreateTime(date);
        employee.setUpdateTime(date);
        int res = employeeMapper.insert(employee);
        if (res == 1) {
            return "redirect:employees";
        } else {
            model.addAttribute("msg", "新增失败");
            return "employee/add";
        }
    }

    @RequestMapping("employees")
    public String list(Model model,
                       @RequestParam(name = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                       @RequestParam(name = "name", required = false, defaultValue = "") String name) {

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByDesc("id");
        pageIndex = pageIndex == null ? 1 : pageIndex;
        pageSize = pageSize == null ? 10 : pageSize;
        Page<Employee> page = new Page<>(pageIndex, pageSize);
        IPage<Employee> iPage = employeeMapper.selectPage(page, queryWrapper);
        List<Employee> employeeList = iPage.getRecords();
        model.addAttribute("employeeList", employeeList);
        model.addAttribute("total", iPage.getTotal());
        model.addAttribute("pages", iPage.getPages());
        model.addAttribute("currentPage", iPage.getCurrent());
        model.addAttribute("pageSize", pageSize);
        return "employee/list";
    }


    @GetMapping("editEmployee")
    public String editEmployee(Model model, @RequestParam(name = "id", required = true, defaultValue = "") String id) {

        Employee employee = employeeMapper.selectById(id);
        model.addAttribute("employee", employee);
        return "employee/edit";
    }

    @PostMapping("editEmployee")
    public String editEmployeeHandler(Model model,
                                      @RequestParam(name = "id", required = true, defaultValue = "") Long id,
                                      @RequestParam(name = "name", required = true, defaultValue = "") String name,
                                      @RequestParam(name = "role", required = true, defaultValue = "") String role,
                                      @RequestParam(name = "sex", required = true, defaultValue = "1") String sex,
                                      @RequestParam(name = "birthday", required = true, defaultValue = "") String birthday,
                                      @RequestParam(name = "age", required = true, defaultValue = "1") Integer age
    ) {

        Date date = new Date();
        Employee employee = new Employee();
        employee.setId(id);
        employee.setName(name);
        employee.setRole(role);
        employee.setSex(sex);
        employee.setBirthday(birthday);
        employee.setAge(age);
        //employee.setCreateTime(date);
        employee.setUpdateTime(date);
        int res = employeeMapper.updateById(employee);
        if (res == 1) {
            return "redirect:employees";
        } else {
            model.addAttribute("msg", "修改失败");
            return "employee/edit";
        }
    }

    @GetMapping("deleteEmployeeById")
    public String deleteEmployeeById(@RequestParam(name = "id", required = true, defaultValue = "") String id) {
        int res = employeeMapper.deleteById(id);
        if (res == 1) {
            return "redirect:employees";
        } else {
            return "delete error";
        }
    }

    @GetMapping("getEmployee")
    @ResponseBody
    public Object getEmployee( @RequestParam(name = "id", required = true, defaultValue = "") String id) {

        List<Employee> employee = employeeMapper.selectList(null);
        RestHelper restHelper =new RestHelper();
        restHelper.setCode(200);
        restHelper.setMsg("成功");
        restHelper.setData(employee);
        return restHelper.toJsonMap();
    }
}
